package base.collision;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Line2D;

import base.common.IBoundingBox;
import base.common.ILiving;
import base.living.Entity;
import base.world.World;

public class Collision
{
	/**Get list of entity that intersects IBoundingBox*/
	public static Entity[] getEntityCollideWith(World par1World, IBoundingBox par2IBoundingBox)
	{
		if (par2IBoundingBox == null)
		{
			return null;
		}

		Entity[] entitylist = new Entity[par1World.livingList.size()];
		int i = 0;

		for (ILiving iliving : par1World.livingList)
		{
			if (iliving instanceof Entity && isCollideWith(par2IBoundingBox, (Entity) iliving))
			{
				entitylist[i] = (Entity) iliving;
				i++;
			}
		}

		return truncateEntityList(entitylist, i);
	}

	/**Return true if par2IBoundingBox intersects par1IBoundingBox*/
	public static boolean isCollideWith(IBoundingBox par1IBoundingBox, IBoundingBox par2IBoundingBox)
	{
		if (par1IBoundingBox == null || par2IBoundingBox == null || par1IBoundingBox == par2IBoundingBox)
		{
			return false;
		}

		return par1IBoundingBox.getBoundingBox().intersects(par2IBoundingBox.getBoundingBox());
	}

	/**Return true if par2Point intersects par1IBoundingBox*/
	public static boolean isCollideWith(IBoundingBox par1IBoundingBox, Point par2Point)
	{
		if (par1IBoundingBox == null)
		{
			return false;
		}

		Rectangle i = par1IBoundingBox.getBoundingBox();

		return par2Point.x > i.getMinX() && par2Point.y > i.getMinY() && par2Point.x < i.getMaxX() && par2Point.y < i.getMaxY();
	}

	/**Return true if par2Line2D intersects par1IBoundingBox*/
	public static boolean isCollideWith(IBoundingBox par1IBoundingBox, Line2D par2Line2D)
	{
		if (par1IBoundingBox == null)
		{
			return false;
		}

		return par1IBoundingBox.getBoundingBox().intersectsLine(par2Line2D);
	}

	/**Return true if par2Rectangle intersects par1IBoundingBox*/
	public static boolean isCollideWith(IBoundingBox par1IBoundingBox, Rectangle par2Rectangle)
	{
		if (par1IBoundingBox == null)
		{
			return false;
		}

		return par1IBoundingBox.getBoundingBox().intersects(par2Rectangle);
	}

	private static Entity[] truncateEntityList(Entity[] par1Entity, int par2)
	{
		Entity[] entitylist = new Entity[par2];
		for (int i = 0; i < par2; i++)
		{
			entitylist[i] = par1Entity[i];
		}

		return entitylist;
	}
}